<?php 
session_start();
require_once 'php_includes.php';

//Make sure logged in
checkLogin();

//make sure it is a manager logged on
checkManager();

    $php_message = "";
    if($_POST['action'] == "remove")
    {
        $sql = "DELETE FROM ReportedErrors WHERE EmployeeID='".$_POST['usertime']."' AND ErrorPeriodStart='".$_POST['period']."' AND CorrectionEvent='".$_POST['errortime']."'";
        
        sqlConnect();
        $sqlresult = sqlQuery($sql);
        sqlExit();
        
        $php_message = "Error report removed";
    }
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Payroll and Timesheet Management Website</title>
<link rel="stylesheet" type="text/css" href="styles.css" />
</head>
<body>
<?php writeHeader(); ?>
<p><span class="pageheader">Errors reported by my employees</span></p>
<?php
    if($php_message != "")
    {
        echo "<p class=\"php_message\">";
        echo $php_message;
        echo "</p>";
    }
?>
<br />
<table class="full">
<tr>
<th></th>
<th></th>
<th></th>
<th>Employee</th>
<th>Period</th>
<th>Event</th>
<th>Event Date</th>
<th>Incorrect Value</th>
<th>Correction</th>
</tr>
<?php
    sqlConnect();
    
    $sql = "SELECT Employee.EmployeeID, EmployeeFName, EmployeeLName, ErrorPeriodStart, Reason, EventTimeCorrection, CorrectionTimestamp, EntryNo, CorrectionEvent FROM ReportedErrors, Employee WHERE Employee.EmployeeID = ReportedErrors.EmployeeID AND ManagerID = '" . $_SESSION['tmsUserID'] . "' ORDER BY CorrectionEvent";
    $results = sqlQuery($sql);
    
    $num_rows = mysql_num_rows($results);
    
    sqlExit();
    
    if($num_rows == 0)
    {
        echo "<tr>\n";
        echo "<td class=\"record\" colspan=\"9\">No Records Available</td>";
        echo "</tr>";   
    }
    else
    {
        for($i = 0; $i < $num_rows; ++$i)
        {
            $data = mysql_fetch_row($results);
            $extract = explode("@", $data[6]);
            $period = explode("-", $data[3]);
            $period2 = explode("-", date("Y-m-d", strtotime($data[3] ." + 13 days")));
            $event_date = explode("-", date("Y-m-d", strtotime($data[3] ." + ".$data[7]." days")));
            
            echo "<tr>\n";
			//if(!$_POST['period'] || !$_POST['action'] || !$_POST['usertime'] || !$_POST['timestamp'] || !$_POST['offset'])
            echo "<td class=\"view\"><form action=\"reporterror.php\" method=\"post\" target=\"submit_time\" onSubmit=\"window.open('', 'submit_time', 'width=450,height=300,status=yes,resizable=yes,scrollbars=yes')\"><div><input type=\"hidden\" value=\"approve\" name=\"action\" /><input type=\"hidden\" value=\"".$data[3]."\" name=\"period\" /><input type=\"hidden\" value=\"".$data[0]."\" name=\"usertime\" /><input type=\"hidden\" value=\"".($data[7]+1)."\" name=\"offset\" /><input type=\"hidden\" value=\"".$data[6]."\" name=\"original\" /><input type=\"hidden\" value=\"".$data[5]."\" name=\"correction\" /><input type=\"hidden\" value=\"".$data[8]."\" name=\"errortime\" /><input type=\"submit\" value=\"Approve\" class=\"button\" /></div></form></td>\n";
            echo "<td class=\"view\"><form action=\"errorsreported.php\" method=\"post\"><div><input type=\"hidden\" value=\"remove\" name=\"action\" /><input type=\"hidden\" value=\"".$data[3]."\" name=\"period\" /><input type=\"hidden\" value=\"".$data[0]."\" name=\"usertime\" /><input type=\"hidden\" value=\"".$data[8]."\" name=\"errortime\" /><input type=\"submit\" value=\"Deny/Edited\" class=\"button\" /></div></form></td>\n";
            echo "<td class=\"view\"><form action=\"timesheet.php\" method=\"post\"><div><input type=\"hidden\" name=\"usertime\"value=\"" . $data[0] . "\" /><input type=\"hidden\" name=\"period\"value=\"" . $data[3] . "\" /><input type=\"submit\" value=\"View\" class=\"button\" /></div></form></td>\n";
            echo "<td class=\"record\">" . $data[1] . " " . $data[2] . "(".$data[0] .")</td>\n";
            echo "<td class=\"record\">".$period[1]."/".$period[2]."/".$period[0]." - " .$period2[1]."/".$period2[2]."/".$period2[0]."</td>\n";
            echo "<td class=\"record\">";
            switch($extract[0])
            {
                case "0":
                    echo "Clock in";
                    break;
                                
                case "1":
                    echo "Clock out";
                    break;
                                
                case "2":
                    echo "Going to lunch";
                    break;
                                
                case "3":
                    echo "Back from lunch";
                    break;
                    
                case "4":
                    echo "Bonus time";
                    break;

                default:
                    echo"Error";
            }
            echo "</td>\n";
            echo "<td class=\"record\">".$event_date[1]."/".$event_date[2]."/".$event_date[0]."</td>\n";
            
            //Source
            echo "<td class=\"record\">";
            $extract2 = explode(" ", $extract[1]);
            $sourcetime = explode(":", $extract2[1]);
            
            //write time
            $period = "AM";
            if($sourcetime[0] >= 12)
            {
                $period = "PM";
                if($sourcetime[0] > 12)
                {
                    $sourcetime[0] = $sourcetime[0] - 12;
                }
            }
            echo (int)$sourcetime[0] . ":" . $sourcetime[1] . " ". $period;
            echo "</td>\n";
        
            //Correction
            echo "<td class=\"record\">";
            if($data[5] =="delete")
            {
                echo "Delete Event";
            }else{
                $extract = explode("@", $data[5]);
                $extract2 = explode(" ", $extract[1]);
                $sourcetime = explode(":", $extract2[1]);
                
                //write time
                $period = "AM";
                if($sourcetime[0] >= 12)
                {
                    $period = "PM";
                    if($sourcetime[0] > 12)
                    {
                        $sourcetime[0] = $sourcetime[0] - 12;
                    }
                }
                echo (int)$sourcetime[0] . ":" . $sourcetime[1] . " ". $period;
                
            }
            echo "</td>\n";
            echo "</tr><tr><td colspan=\"3\"></td>";
            echo "<td class=\"record\"colspan=\"6\">" . $data[4] . "</td>\n";
            echo "</tr>\n";
        }
    }
?>
</table>
</body>
</html>
